package com.hankai.controller.backend;

import com.hankai.common.Const;
import com.hankai.common.ResponseCode;
import com.hankai.common.ServerResponse;
import com.hankai.pojo.User;
import com.hankai.service.IOrderService;
import com.hankai.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Created by hankai on 2017/6/16.
 */
@Controller
@RequestMapping("/manage/order/")
public class OrderManageController {
    @Resource
    private IUserService iUserService;
    @Resource
    private IOrderService iOrderService;

    /**
     * 管理员查询所有订单列表
     * @param session
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "list.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse list(HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        ServerResponse response = iUserService.checkIsNotAdmin(user);
        if(!response.isSuccess()){
            return response;
        }
        return iOrderService.manageList(pageNum,pageSize);
    }

    @RequestMapping(value = "orderDetail.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse orderDetail(HttpSession session,long orderNo){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        ServerResponse response = iUserService.checkIsNotAdmin(user);
        if(!response.isSuccess()){
            return response;
        }
        return iOrderService.orderDetail(null,orderNo);
    }

    /**
     * 查询订单
     * @param session
     * @param orderNo
     * @return
     */
    @RequestMapping(value = "orderSearch.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse orderSearch(HttpSession session,long orderNo,@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        ServerResponse response = iUserService.checkIsNotAdmin(user);
        if(!response.isSuccess()){
            return response;
        }
        return iOrderService.orderSearch(orderNo,pageNum,pageSize);
    }

    /**
     * 发货
     * @param session
     * @param orderNo
     * @return
     */
    @RequestMapping(value = "sendGoods.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse sendGoods(HttpSession session,long orderNo){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        ServerResponse response = iUserService.checkIsNotAdmin(user);
        if(!response.isSuccess()){
            return response;
        }
        return iOrderService.manageSendGoods(orderNo);
    }

}
